package cs2340.g21.wms;

import java.io.IOException;
import java.io.Serializable;
import android.location.Location;

/**
 * A wrapper class that makes Location serializable
 */
public class SerializableLocation implements Serializable {
	
	private static final long serialVersionUID = 1467234308324905631L;
	
	private Location location;
	private String locality;
	
	/**
	 * @param loc Location to set this wrapper's location to
	 */
	public void setLocation(Location loc) {
		location = loc;
	}
	
	/**
	 * @return the location object wrapped by this class
	 */
	public Location getLocation() {
		return location;
	}
	
	/**
	 * @return The longitude of the location wrapped by this class
	 */
	public double getLongitude() {
		return location.getLongitude();
	}
	
	 /**
      * @return The latitude of the location wrapped by this class
      */
	public double getLatitude() {
		return location.getLatitude();
	}
	
	/**
	 * @param loc sets locality
	 */
	public void setLocality(String loc) {
		locality = loc;
	}
	
	/**
	 * @return gets the locality, a short string that describes the Location
	 */
	public String getLocality() {
		return locality;
	}
	
	
	/**
     * Used to serialize location
     * @param out outputstream to which location will be written
     * @throws IOException
     */
    private void writeObject(java.io.ObjectOutputStream out) throws IOException{
            if (location != null) {
            	final double lat = location.getLatitude();
            	final double lon = location.getLongitude();
            	final String prov = location.getProvider();
            	final String loc = locality;
            	out.writeInt(1); // a location has been written
            	out.writeDouble(lat);
            	out.writeDouble(lon);
            	out.writeObject(prov);
            	out.writeObject(loc);
            }
    }
    
    /**
     * Used to recreate location from an output stream
     * @param in inputstream from which location will be read
     * @throws IOException
     * @throws ClassNotFoundException
     */
    private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException{
        if (in.readInt() == 1) {
        	final double lat = in.readDouble();
        	final double lon = in.readDouble();
        	String prov = in.readObject().toString();
        	location = new Location(prov);
        	location.setLatitude(lat);
        	location.setLongitude(lon);
        	locality = in.readObject().toString();
        }
    }

}
